package cn.b.training.baby_education_training.api;

import cn.b.training.baby_education_training.entity.FoodMenu;
import cn.b.training.baby_education_training.repository.AgeTagRepository;
import cn.b.training.baby_education_training.repository.ClassifyTagRepository;
import cn.b.training.baby_education_training.repository.FoodMenuRepository;
import cn.b.training.baby_education_training.repository.SickTagRepository;
import cn.b.training.baby_education_training.service.FoodMenuService;
import cn.b.training.baby_education_training.service.dto.FoodMenuDto;
import cn.b.training.baby_education_training.service.dto.MenuDto;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @program: baby_education_training
 * @description
 * @author: wuying
 * @create: 2022-12-02 11:58
 **/
@RestController
@RequestMapping("/foodMenu")
public class FoodMenuController {
    @Autowired
    private FoodMenuService foodMenuService;
    @Autowired
    private AgeTagRepository ageTagRepository;
    @Autowired
    private SickTagRepository sickTagRepository;
    @Autowired
    private ClassifyTagRepository classifyTagRepository;
    @Autowired
    private FoodMenuRepository foodMenuRepository;

    @PostMapping
    public ResponseEntity addFoodMenu(@Valid FoodMenuDto dto){
        try {
            boolean b = foodMenuService.addFoodMenu(dto);
            if (b){
                JSONObject result = new JSONObject();
                result.put("success","添加成功");
                return ResponseEntity.ok(result);
            }else {
                JSONObject error = new JSONObject();
                error.put("failed","添加失败");
                return ResponseEntity.status(400).body(error);
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @GetMapping
    public ResponseEntity getFoodMenuById(Long id) {
        try {
            MenuDto dto = this.foodMenuService.getFoodMenuById(id);
            return ResponseEntity.ok(dto);
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error", e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @GetMapping("/title")
    public ResponseEntity getFoodMenuByTitle(String foodTitle) {
        try {
            String newfoodTitle = "%"+foodTitle+"%";
            List<FoodMenu> foodMenu = this.foodMenuService.getFoodMenuByTitle(newfoodTitle);
            return ResponseEntity.ok(foodMenu);
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error", e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @DeleteMapping
    public ResponseEntity DeleteFoodMenuById(Long id){
        try {
            boolean b = foodMenuService.deleteFoodMenuById(id);
            if (b) {
                JSONObject result = new JSONObject();
                result.put("success", "删除成功");
                return ResponseEntity.ok(result);
            }else {
                JSONObject error = new JSONObject();
                error.put("failed","删除失败");
                return ResponseEntity.status(400).body(error);
            }
        }catch (Exception e){
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @GetMapping("/all")
    public ResponseEntity getFoodMenus(@RequestParam(required = false, defaultValue = "0") Integer page,
                                      @RequestParam(required = false, defaultValue = "10") Integer size) {
        Page<FoodMenu> result = this.foodMenuService.getAllMenus(page, size);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/age/all")
    public ResponseEntity getAges() {
        return ResponseEntity.ok(ageTagRepository.findAll());
    }
    @GetMapping("/sick/all")
    public ResponseEntity getSick() {
        return ResponseEntity.ok(sickTagRepository.findAll());
    }
    @GetMapping("/classify/all")
    public ResponseEntity getClassify() {
        return ResponseEntity.ok(classifyTagRepository.findAll());
    }

    @GetMapping("/ageTag")
    public ResponseEntity getFoodByAgeId(@RequestParam Long ageTagId){
        return ResponseEntity.ok(foodMenuRepository.getByAgeId(ageTagId));
    }

    @GetMapping("/sickTag")
    public ResponseEntity getFoodBySickId(@RequestParam Long sickTagId){
        return ResponseEntity.ok(foodMenuRepository.getBySickId(sickTagId));
    }

    @GetMapping("/classifyTag")
    public ResponseEntity getFoodByClassifyId(@RequestParam Long classifyTagId){
        return ResponseEntity.ok(foodMenuRepository.getByClassifyId(classifyTagId));
    }
}
